package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.ChoiceData;
import com.banzhi.shop.data.GroupingIndexData;
import com.banzhi.shop.model.CallBack.ChoiceCallBack;
import com.banzhi.shop.model.IChoiceModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/9.
 */

public class ChoiceImpl implements IChoiceModel {
    @Override
    public void getData(String id, String page, String pagesize, final ChoiceCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceUrl);
        params.addBodyParameter("id", id + "");
        params.addBodyParameter("page", page + "");
        params.addBodyParameter("pagesize", pagesize + "");
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.e("ChoiceImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.e("result", result.toString());
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                     //   GroupingIndexData groupingIndexData = gson.fromJson(result, GroupingIndexData.class);
                        ChoiceData choiceData = gson.fromJson(result, ChoiceData.class);
                        callBack.ongetChoiceDataSuccess(choiceData);
                        //    callBack.ongetGroupingDataSuccess(groupingIndexData.getData());
                    } else {
                    //    callBack.ongetGroupingDataError(jsonobject);
                        callBack.ongetChoiceDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("ChoiceImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(1011)");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
